package cn.com.huak.www.sell.service.impl;

import cn.com.huak.www.sell.dto.OrderDTO;
import cn.com.huak.www.sell.service.PayService;
import cn.com.huak.www.sell.utils.JsonUtil;
import com.lly835.bestpay.config.WxPayH5Config;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.service.BestPayService;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 支付接口
 * @author huak@wanggang
 * @date 2019/1/15 2:27
 */
@Service
@Slf4j
public class PayServiceImpl implements PayService {

    private static final String ORDER_NAME = "微信点餐系统订单名称";

    @Autowired
    private BestPayService bestPayService;

    /**
     * 创建订单
     * @param orderDTO 订单详情
     */
    @Override
    public PayResponse create(OrderDTO orderDTO) {

        PayRequest payRequest = new PayRequest();
        payRequest.setOpenid(orderDTO.getBuyerOpenid());
        payRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        payRequest.setOrderId(orderDTO.getId());
        payRequest.setOrderName(ORDER_NAME);
        payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);

        log.info("【微信支付】request={}", JsonUtil.toJson(payRequest));
        //1. 发起支付请求
        PayResponse pay = bestPayService.pay(payRequest);
        log.info("【微信支付】response={}", JsonUtil.toJson(pay));

        // 返回结果
        return pay;
    }
}
